package test;
import common.FileTransferMessage;

import ocsf.server.*;
import ocsf.client.*;

public class testFileTransfer extends Thread {

  public static void main(String[] args) {
    
    System.out.println("*** File Transfer tests ***\n");

    Thread test= new testFileTransfer();
    test.start();
  }

  Server server;
  Client client1;
  Client client2;
  
  public testFileTransfer() {
  
    server= new Server(12345);
    client1= new Client("localhost",12345);  
    client2= new Client("localhost",12345);  
  }

  public void run() {
  
    try {

     // Server listens    
     System.out.println("-- Server listens --");
     System.out.println("Server.getPort()="+server.getPort());
     System.out.println("Server.isListening()="+server.isListening());
     server.listen();
     Thread.sleep(1000);
     System.out.println("\n");
    
     // Client 1 connects
     System.out.println("-- Client 1 connects --");
     System.out.println("Client.isConnected()="+client1.isConnected());
     client1.openConnection();
     System.out.println("Client.getInetAddress()="+client1.getInetAddress()); 
     Thread.sleep(1000);
     System.out.println("\n");

     // Client 2 connects
     System.out.println("-- Client 2 connects --");
     client2.openConnection();
     // create File transfer message
     FileTransferMessage msg = new FileTransferMessage("/home/milchtilde/Documents/tmp/testfile.txt", client1.getInetAddress().toString());
     client1.sendToServer(msg.getParsedCommandString());
     Thread.sleep(1000);
     System.out.println("\n");
    
     // Server closes
     System.out.println("-- Server closes --");
     System.out.println("iv)Server.getNumberOfClients()="+server.getNumberOfClients());
     server.close();
     
    } catch (Exception ex) {
    
      System.out.println("Server exception: " + ex);
    }
  }


private class Server extends AbstractServer {

  Server(int p) {
  
    super(p);
  }
  
  protected void serverStarted() {

    System.out.println("Server: Started");
    System.out.println("Server.isListening()="+isListening());
    System.out.println("Server.isClosed()="+isClosed());
    System.out.println("Server.getNumberOfClients()="+getNumberOfClients());
  }
  
  protected void serverStopped() {
  
    System.out.println("Server: Stopped");
    System.out.println("Server.isListening()="+isListening());
    System.out.println("Server.isClosed()="+isClosed());
    System.out.println("Server.getNumberOfClients()="+getNumberOfClients());
  }

  protected void serverClosed() {
  
    System.out.println("Server: Closed");
    System.out.println("Server.isListening()="+isListening());
    System.out.println("Server.isClosed()="+isClosed());
    System.out.println("Server.getNumberOfClients()="+getNumberOfClients());
  }
  
  protected void clientConnected(ConnectionToClient client) {
  
    System.out.println("Server: client #" + getNumberOfClients() + " connected");
  }

  synchronized protected void clientDisconnected(
    ConnectionToClient client) {  

    System.out.println("Server: client disconnected");    
  }

  synchronized protected void clientException(
    ConnectionToClient client, Throwable exception) {
  
    System.out.println("Connection to client exception: " + exception);    
  }
  
  protected void listeningException(Throwable exception) {
  
    System.out.println("Listening exception: " + exception);    
  }
  
  protected void handleMessageFromClient(Object msg,
                               ConnectionToClient client){
                               
    System.out.println("Server: Message received=" + msg);
  }
}
  
private class Client extends AbstractClient {

  Client(String h, int p) {
  
    super(h,p);
  }
  
  protected void connectionClosed() {

    System.out.println("Client: Closed");
    System.out.println("Client.isConnected()="+isConnected());
  }
  
  protected void connectionException(Exception exception) {
  
    System.out.println("Client exception: " + exception);
  }
  
  protected void connectionEstablished() {

    System.out.println("Client: Connected");
    System.out.println("Client.isConnected()="+isConnected());
  }

  protected void handleMessageFromServer(Object msg){

    System.out.println("Client: Message received=" +msg);
  }
}
}
